﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CompressDataSet;
using System.IO;

namespace LearnDrive
{
    public partial class frmUpdateDB : Form
    {
        WebReference2.Heart s = new WebReference2.Heart();
        public frmUpdateDB()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            ProgressBar.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            
        }

        private void frmUpdateDB_Load(object sender, EventArgs e)
        {
            updateDB();

        }
        private void updateDB()
        {
            ThreadStart start = null;
            Thread t = null;
            if (start == null)
            {
                start = getupdate;
            }
            t = new Thread(start);
            t.IsBackground = true;
            t.Start();
        }
        private void getupdate()
        {
            DateTime dttk = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_tiku");
            DateTime dtzj = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_zhangjies");
            DateTime dtdel = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_tikudel");
            DateTime dtdelgg = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_gonggaodel");
            DateTime dtdelzjs = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_zhangjiesdel");
            DateTime dtgg = AShelp.getMaxTime("max(GengXinShiJian)", "tbl_gonggao");
            DateTime dttp = AShelp.getMaxTimeTS("max(GengXinShiJian)", "tbl_shengyintp");
            DataSet dstk = gettest("id", "tbl_tiku", "GengXinShiJian>'" + dttk.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dszj = gettest("id", "tbl_zhangjies", "GengXinShiJian>'" + dtzj.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dsdel = gettest("id,GengXinShiJian,sign", "tbl_tikudel", "GengXinShiJian>'" + dtdel.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dsdelgg = gettest("id,GengXinShiJian,sign", "tbl_gonggaodel", "GengXinShiJian>'" + dtdelgg.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dsdelzjs = gettest("id,GengXinShiJian,sign", "tbl_zhangjiesdel", "GengXinShiJian>'" + dtdelzjs.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dsgg = gettest("id", "tbl_gonggao", "GengXinShiJian>'" + dtgg.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            DataSet dstp = gettest("Fname", "tbl_shengyintp", "GengXinShiJian>'" + dttp.ToString("yyyy-MM-dd HH:mm:ss") + "' order by GengXinShiJian asc");
            int count = 0;
            if (dstk != null) count += dstk.Tables[0].Rows.Count;
            if (dszj != null) count += dszj.Tables[0].Rows.Count;
            if (dsdel != null) count += dsdel.Tables[0].Rows.Count;
            if (dsgg != null) count += dsgg.Tables[0].Rows.Count;
            if (dsdelgg != null) count += dsdelgg.Tables[0].Rows.Count;
            if (dsdelzjs != null) count += dsdelzjs.Tables[0].Rows.Count;
            if (dstp != null) count += dstp.Tables[0].Rows.Count;
            if (count > 0)
            {
                //progressBar1.Visible = true;
                progressBar1.Maximum = count;
                progressBar1.Value = 0;

                DataSet temp;
                if (dstk != null)
                    for (int i = 0; i < dstk.Tables[0].Rows.Count; i++)
                    {
                        temp = gettest("id,zhangjie,zhangjie_s,tixing,neirong,jiangjie,daan,shengyin,tupian,jichu,qianghua,sucheng,jiaoan,zhuanxiang,zhangjie_k,cccc,aab,ab,def,huifu,km1,km4,gengxinshijian,a,b,c,d,e,f,iswenmang", "tbl_tiku", "id=" + dstk.Tables[0].Rows[i]["id"].ToString());
                        if (temp != null && temp.Tables[0].Rows.Count == 1)
                        {
                            AShelp.insertTiKu(temp.Tables[0].Rows[0]["id"].ToString(), temp.Tables[0].Rows[0]["zhangjie"].ToString(), temp.Tables[0].Rows[0]["zhangjie_s"].ToString(), temp.Tables[0].Rows[0]["tixing"].ToString(), temp.Tables[0].Rows[0]["neirong"].ToString(), temp.Tables[0].Rows[0]["jiangjie"].ToString(), temp.Tables[0].Rows[0]["daan"].ToString(), temp.Tables[0].Rows[0]["shengyin"].ToString(), temp.Tables[0].Rows[0]["tupian"].ToString(), temp.Tables[0].Rows[0]["jichu"].ToString(), temp.Tables[0].Rows[0]["qianghua"].ToString(), temp.Tables[0].Rows[0]["sucheng"].ToString(), temp.Tables[0].Rows[0]["jiaoan"].ToString(), temp.Tables[0].Rows[0]["zhuanxiang"].ToString(), temp.Tables[0].Rows[0]["zhangjie_k"].ToString(), temp.Tables[0].Rows[0]["cccc"].ToString(), temp.Tables[0].Rows[0]["aab"].ToString(), temp.Tables[0].Rows[0]["ab"].ToString(), temp.Tables[0].Rows[0]["def"].ToString(), temp.Tables[0].Rows[0]["huifu"].ToString(), temp.Tables[0].Rows[0]["km1"].ToString(), temp.Tables[0].Rows[0]["km4"].ToString(), temp.Tables[0].Rows[0]["gengxinshijian"].ToString(), temp.Tables[0].Rows[0]["a"].ToString(), temp.Tables[0].Rows[0]["b"].ToString(), temp.Tables[0].Rows[0]["c"].ToString(), temp.Tables[0].Rows[0]["d"].ToString(), temp.Tables[0].Rows[0]["e"].ToString(), temp.Tables[0].Rows[0]["f"].ToString(), temp.Tables[0].Rows[0]["iswenmang"].ToString());
                        }
                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }

                if (dszj != null)
                    for (int i = 0; i < dszj.Tables[0].Rows.Count; i++)
                    {
                        temp = gettest("id,bianhao,mingcheng,kemu1,kemu2,huifu,cccc,aab,ab,def,jichu,qianghua,sucheng,jiaoan,zhuanxiang,zhangjie,sign,gengxinshijian,iswenmang", "tbl_zhangjies", "id=" + dszj.Tables[0].Rows[i]["id"].ToString());
                        if (temp != null && temp.Tables[0].Rows.Count == 1)
                        {
                            AShelp.insertZhangjies(temp.Tables[0].Rows[0]["id"].ToString(), temp.Tables[0].Rows[0]["bianhao"].ToString(), temp.Tables[0].Rows[0]["mingcheng"].ToString(), temp.Tables[0].Rows[0]["kemu1"].ToString(), temp.Tables[0].Rows[0]["kemu2"].ToString(), temp.Tables[0].Rows[0]["huifu"].ToString(), temp.Tables[0].Rows[0]["cccc"].ToString(), temp.Tables[0].Rows[0]["aab"].ToString(), temp.Tables[0].Rows[0]["ab"].ToString(), temp.Tables[0].Rows[0]["def"].ToString(), temp.Tables[0].Rows[0]["jichu"].ToString(), temp.Tables[0].Rows[0]["qianghua"].ToString(), temp.Tables[0].Rows[0]["sucheng"].ToString(), temp.Tables[0].Rows[0]["jiaoan"].ToString(), temp.Tables[0].Rows[0]["zhuanxiang"].ToString(), temp.Tables[0].Rows[0]["zhangjie"].ToString(), temp.Tables[0].Rows[0]["sign"].ToString(), temp.Tables[0].Rows[0]["gengxinshijian"].ToString(), temp.Tables[0].Rows[0]["iswenmang"].ToString());
                        }
                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                if (dsdel != null)
                    for (int i = 0; i < dsdel.Tables[0].Rows.Count; i++)
                    {

                        AShelp.insertDel(dsdel.Tables[0].Rows[i]["id"].ToString(), dsdel.Tables[0].Rows[i]["GengXinShiJian"].ToString(), dsdel.Tables[0].Rows[i]["sign"].ToString(), "tbl_tikudel");

                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                if (dsgg != null)
                    for (int i = 0; i < dsgg.Tables[0].Rows.Count; i++)
                    {
                        temp = gettest("id,biaoti,neirong,zuozhe,shijian,jiezhiriqi,gengxinshijian", "tbl_gonggao", "id=" + dsgg.Tables[0].Rows[i]["id"].ToString());
                        if (temp != null && temp.Tables[0].Rows.Count == 1)
                        {
                            AShelp.insertGonggao(temp.Tables[0].Rows[0]["id"].ToString(), temp.Tables[0].Rows[0]["biaoti"].ToString(), temp.Tables[0].Rows[0]["neirong"].ToString(), temp.Tables[0].Rows[0]["zuozhe"].ToString(), temp.Tables[0].Rows[0]["shijian"].ToString(), temp.Tables[0].Rows[0]["jiezhiriqi"].ToString(), temp.Tables[0].Rows[0]["gengxinshijian"].ToString());
                        }
                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                if (dsdelgg != null)
                    for (int i = 0; i < dsdelgg.Tables[0].Rows.Count; i++)
                    {

                        AShelp.insertDel(dsdelgg.Tables[0].Rows[i]["id"].ToString(), dsdelgg.Tables[0].Rows[i]["GengXinShiJian"].ToString(), dsdelgg.Tables[0].Rows[i]["sign"].ToString(), "tbl_gonggaodel");

                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                if (dsdelzjs != null)
                    for (int i = 0; i < dsdelzjs.Tables[0].Rows.Count; i++)
                    {

                        AShelp.insertDel(dsdelzjs.Tables[0].Rows[i]["id"].ToString(), dsdelzjs.Tables[0].Rows[i]["GengXinShiJian"].ToString(), dsdelzjs.Tables[0].Rows[i]["sign"].ToString(), "tbl_zhangjiesdel");

                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                if (dstp != null)
                    for (int i = 0; i < dstp.Tables[0].Rows.Count; i++)
                    {
                        temp = gettest("fname,fneirong,sign,gengxinshijian", "tbl_shengyintp", "fname='" + dstp.Tables[0].Rows[i]["fname"].ToString() + "'");
                        if (temp != null && temp.Tables[0].Rows.Count == 1)
                        {
                            AShelp.insertTP(temp.Tables[0].Rows[0]["fname"].ToString(), (byte[])temp.Tables[0].Rows[0]["fneirong"], temp.Tables[0].Rows[0]["sign"].ToString(), temp.Tables[0].Rows[0]["gengxinshijian"].ToString());
                        }
                        progressBar1.Value++;
                        progressBar1.Refresh();
                        this.Text = "数据更新" + progressBar1.Value + "/" + progressBar1.Maximum;
                    }
                //progressBar1.Visible = false;
            }
            this.Close();
        }
        private DataSet gettest(string sel, string tab, string where)
        {
            try {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                byte[] buffer = s.GetUpdata(sel, tab, " where " + where);
                BinaryFormatter bf = new BinaryFormatter();
                DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;
                DataSet ds = dss.ConvertToDataSet();
                sw.Stop();
                return ds;
            }
            catch (Exception ex)
            {
                AShelp.writeLog(ex.Message);
                return null;
            }
        }
    }
}
